const app = getApp()
const $ = app.globalData
import { $wuxKeyBoard } from '../../../dist/index'
var WxParse = require('../../../wxParse/wxParse.js');
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		is_shoucang: 0,
		goods_img: ['http://iph.href.lu/750x750'],
		indicatorDots: true,
		autoplay: true,
		interval: 5000,
		duration: 1000,
		anim: false,
		flist: false,
		ggindex: 0,
		goodsnub: 1,
		shopid:0,
		shopname:"",
		miaosha:true,
		kucun:1,
		yunfei:1
	},
	// 规格点击事件
	ggclick: function(e) {
		var index = e.target.dataset.index
		var ggid = e.target.dataset.id
		var that = this
		that.setData({
			ggindex: index,
			ggid: ggid,
			jiage: that.data.goodsinfo.goodsSpecList[index].vipcashprice,
			jiage2:that.data.goodsinfo.goodsSpecList[index].vipintegralprice,
			jiage3:that.data.goodsinfo.goodsSpecList[index].plainprice
		})
	},
	// 图片点击事件
	previewImage: function(e) {
		var current = e.target.dataset.src;
		var goodsimg = this.data.goods_img;
		var imglist = [];
		for (var i = 0; i < goodsimg.length; i++) {
			imglist[i] = goodsimg[i]
		}
		console.log(goodsimg)
		wx.previewImage({
			current: current, // 当前显示图片的http链接  
			urls: imglist // 需要预览的图片http链接列表  
		})
	},
	
	buy: function() {
		this.setData({
			anim: true,
			flist: true
		})
	},
	closebg: function() {
		console.log(1)
		this.setData({
			anim: false
		})
	},
	// 输入密码
	// open() {
	//     $wuxKeyBoard().show({
	//         callback(value) {
	//             console.log(`输入的密码是：${value}`)
	//             return true
	//         },
	//     })
	// },
	// 加入购物车
	jiaruchart: function() {
		var that = this
		wx.getStorage({
			key:"userinfo",
			success:function(res){
				wx.request({
					url:$.AdminUrl+"/YApi/addToMyShopcar",
					data:{
						customerid:res.data.id,
						goodsid:that.data.goodsid,
						count:that.data.goodsnub,
						specid:that.data.ggid,
						saletype:that.data.goodsinfo.shoptype,
						shopid:that.data.shopid
					},
					success:function(res){
						console.log(res)
						if(res.data.code){
							wx.showToast({
								title: res.data.msg,
								duration: 3000,
							})
							that.setData({
								anim: false
							})
						}else{
							wx.showToast({
								icon:'none',
								title: res.data.msg,
								duration: 3000,
							})
						}
					}
				})
				
			}
		})
	},
	// 立即购买
	lijibuy: function() {
		var that = this
		var order = {}
			order['shopid'] = that.data.shopid
			order['goods'] = that.data.goodsid
			order['shopname'] = that.data.shopname
			order['goodsimg'] = that.data.goodsinfo.photo
			order['goodstitle'] = that.data.goodsinfo.name
			order['jiage'] = that.data.jiage
			order['jiage2'] = that.data.jiage2
			order['guige'] = that.data.goodsinfo.goodsSpecList[that.data.ggindex].name
			order['nub'] = that.data.goodsnub
			order['guigeid'] = that.data.ggid
			order['shoptype'] = that.data.goodsinfo.shoptype
			order['goodstype'] = that.data.goodsinfo.shoptype
			order['jiage3'] = that.data.jiage3
			order['yunfei'] = that.data.goodsinfo.fareprice
			$.orderarr = order
			wx.navigateTo({
				url:"../../payment/payment?daizhuce="+that.data.daizhuce
			})
		
	},
	
	//库存
	kucun:function(e){
		this.setData({
			kucun: e.detail.value
		})
	},
	
	// 运费
	yunfei:function(e){
		this.setData({
			yunfei: e.detail.value
		})
	},
	// 加入库存
	jiarukucun:function(){
		var that = this
		wx.showLoading({
			title: '请稍等...',
		})
		wx.request({
			url:$.AdminUrl+"/YApi/addToMyshop",
			data:{
				shopid:$.myshopid,
				goodsid:that.data.goodsid,
				stock:that.data.kucun,
				fareprice:that.data.yunfei,
			},
			success:function(data){
				wx.hideLoading()
				wx.showToast({
					title: "加入成功",
					duration: 3000,
				})
			}
		})
	},
	// 收藏
	shoucang:function(e){
		wx.showLoading({
			title: '请稍等...',
		})
		var that = this
		var goodsid = e.currentTarget.dataset.id
		if(!that.data.userinfo){
			wx.showToast({
				icon:'none',
				title: "请登陆后进行操作",
				duration: 3000,
			})
		}else{
			wx.request({
				url:$.AdminUrl+"/YApi/collectGoods",
				data:{
					customerid:that.data.userinfo.id,
					goodsid:goodsid
				},
				success:function(res){
					wx.hideLoading()
					if(res.data.code){
						// 收藏成功
						wx.showToast({
							title: res.data.msg,
							duration: 3000,
						})
						that.setData({
							collectstate:1
						})
					}else{
						// 收藏失败
						wx.showToast({
							icon:'none',
							title: "收藏失败",
							duration: 3000,
						})
					}
				},
				fail:function(){
					wx.hideLoading()
				}
			})
		}
	},
	// 去首页
	goindex:function(){
		
		wx.switchTab({
			url:"/pages/index/index"
		})
	},
	// 去购物车
	goshopcart:function(){
		
		wx.switchTab({
			url:"/pages/shoppingcart/shoppingcart"
		})
	},
	quxiaoshoucang:function(e){
		wx.showLoading({
			title: '请稍等...',
		})
		var that = this
		var goodsid = e.currentTarget.dataset.id
		wx.request({
			url:$.AdminUrl+"/YApi/cancelCollectGoods",
			data:{
				customerid:that.data.userinfo.id,
				goodsid:goodsid
			},
			success:function(res){
				wx.hideLoading()
				if(res.data.code){
					// 收藏成功
					wx.showToast({
						title: res.data.msg,
						duration: 3000,
					})
					that.setData({
						collectstate:0
					})
				}else{
					wx.showToast({
						icon:'none',
						title: "发生错误，请重试",
						duration: 3000,
					})
				}
			},
				fail:function(){
					wx.hideLoading()
				}
		})
	},
	
	thisnub: function(e) {
		this.setData({
			goodsnub: e.detail.value
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		var that = this
		var goodsid = options.id;
		var daizhuce = options.daizhuce?options.daizhuce:false
		var jinhuo = options.jinhuo?options.jinhuo:false
		var goodslib = options.goodslib;
		$.userinfo = wx.getStorageSync("userinfo")
		that.setData({
			userinfo:$.userinfo,
			daizhuce:daizhuce,
			jinhuo:jinhuo,
			shoptype:$.shoptype
		})
		console.log($.userinfo)
		if(goodslib == 0){
			that.setData({
				goodsid: goodsid,
				goodslib: goodslib,
			})
			wx.request({
				url: $.AdminUrl + "/YApi/getGoodsById",
				data: {
					goodsid: goodsid,
					shopid: 0,
					customerid:$.userinfo?$.userinfo.id:''
				},
				success: function(data) {
					var dataa = data.data.obj.goods
					var banner = dataa.banner.split(",")
					if(dataa.shoptype == 5){
						var miaoshaa = setInterval(function(){
							var date1 = new Date(dataa.endtime)
							var date2 = new Date()
							var shijianchuo = date1.getTime() - date2.getTime()
							if(shijianchuo<=0){
								var miaosha = false
								that.setData({
									miaosha:miaosha
								})
								clearInterval(miaoshaa)
							}else{
								// var time = new Date(shijianchuo);
								var d = parseInt(shijianchuo/86400000);
								var h = parseInt((shijianchuo%86400000)/3600000);
								var mm = parseInt((shijianchuo%86400000%3600000)/60000);
								var s = parseInt((shijianchuo%86400000%3600000%60000)/1000);
								that.setData({
									timed:d,
									timeh:h,
									timemm:mm,
									times:s
								})
							}
						},1000)
					}
					var article = dataa.content
					console.log(article)
					WxParse.wxParse('article', 'html', article, that,5);
					that.setData({
						goodsinfo: dataa,
						collectstate:data.data.obj.collectstate,
						goods_img: banner,
						ggid: dataa.goodsSpecList[0].id,
						jiage: dataa.goodsSpecList[0].vipcashprice,
						jiage2:dataa.goodsSpecList[0].vipintegralprice,
						jiage3:dataa.goodsSpecList[0].plainprice
					})
					console.log(data)
				}
			})
		}else{
			var shopname = options.shopname;
			var shopid = options.shopid;
			that.setData({
				goodsid: goodsid,
				shopid: shopid,
				shopname:shopname
			})
			wx.request({
				url: $.AdminUrl + "/YApi/getGoodsById",
				data: {
					goodsid: goodsid,
					shopid: shopid,
					customerid:$.userinfo?$.userinfo.id:''
				},
				success: function(data) {
					var dataa = data.data.obj.goods
					var banner = dataa.banner.split(",")
					var article = dataa.content
					console.log(article)
					WxParse.wxParse('article', 'html', article, that,5);
					that.setData({
						goodsinfo: dataa,
						collectstate:data.data.obj.collectstate,
						goods_img: banner,
						ggid: dataa.goodsSpecList[0].id,
						jiage: dataa.goodsSpecList[0].vipcashprice,
						jiage2:dataa.goodsSpecList[0].vipintegralprice,
						jiage3:dataa.goodsSpecList[0].plainprice
					})
					console.log(data)
				}
			})
		}
	},
	onShow(){
		// this.onLoad()
		this.setData({
			userinfo:$.userinfo?$.userinfo:''
		})
		console.log($.userinfo)
	},
	// 去登录
	gologin:function(){
		wx.navigateTo({
			url:"../../login/login"
		})
	},
	// 分享转发
		onShareAppMessage: function() {
			var userinfo = wx.getStorageSync('userinfo')
			var that = this
			return {
				title: '优悦购生活管家',
				path: '/pages/shopping/details/details?userid='+userinfo.loginname+"&id="+that.data.goodsid+"&goodslib="+that.data.goodslib+"&daizhuce="+that.data.daizhuce+"&shopname="+that.data.shopname+"&shopid="+that.data.shopid,
				success: (res) => {
					console.log("转发成功", res);
				},
				fail: (res) => {
					console.log("转发失败", res);
				}
			}
		}
})
